package data;

import java.util.List;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.criterion.*;
import org.hibernate.service.*;
import org.hibernate.Criteria;
import businessobjects.*;


public class QuestionDal implements IQuestionDal{
	
	static SessionFactory sessionFactory;
	static ServiceRegistry serviceRegistry;
	Configuration conf = new Configuration().configure();


	@Override
	public Test getTest(String kategorie, int size) {
		Test dummy = null;
		List questions = getQuestions(kategorie);
		return dummy;
	}
	
	private List getQuestions(String kategorie) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Criteria cr = session.createCriteria(Question.class);
		if(kategorie != "all"){
			cr.add(Restrictions.like("kategorie", kategorie));
		}
		List results = cr.list();
		session.close();
		return (List) results;
	}

	@Override
	public boolean insertQuestion(Question question) {
		// TODO Auto-generated method stub
		Session insert = sessionFactory.openSession();
		Transaction tx = null;
		try{
			  tx = insert.beginTransaction();
		      Question newQuest = new Question(question.getKategorie(), question.getQuestion(), question.getAnswer());
		      insert.save(newQuest);
		      tx.commit();
		}catch (HibernateException e) {
			  if (tx!=null) tx.rollback();
			  e.printStackTrace();
			  return false;
		}finally {
			  insert.close();
		}
		return true;
	}


}
